<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>实现优先队列</title>
</head>
<body>
<script>
    class Element {
        constructor(value, priority) {
            this.value = value
            this.priority = priority
        }
    }
    class Priority {

        constructor() {
            this.item = []
        }

        insert(element, priority) {
            const ele = new Element(element, priority);
            const len = this.item.length;

            let add = false;
            for (let i = 0; i < len;  i++) {
                if (this.item[i].priority > ele.priority) {
                    this.item.splice(i, 0, ele)
                    add = true
                    break;
                }
            }
            if (!add) {
                this.item.push(ele)
            }
        }

        print() {
            console.log(this.item)
        }

    }

    const queue = new Priority()
    queue.insert('a', 1)
    queue.insert('b', 2)
    queue.insert('c', 2)
    queue.insert('d', 1)
    queue.print()


</script>
</body>
</html>